<template>
    <el-card shadow="never" style="border: 0;">
        <yun-form
                :data="row"
                @submit="onSubmit"
                ref="yunform"
                :columns="columns">
                <template #default>
                    {:token_field()}
                </template>
                <template #price="{rows}">
                    <el-form-item label="默认价格" prop="price">
                        <el-input v-model="rows.price" :min="0" :step="0.01">
                            <template #append>元/{{rows.unit}}</template>
                        </el-input>
                    </el-form-item>
                </template>
        </yun-form>
    </el-card>
</template>
<script>
    import form from "@components/Form.js";
    export default{
        components:{
            'YunForm':form
        },
        data:{
            columns:[
                {"field":"id","title":"ID",width:80,"edit":"hidden"},
                {"field":"name","title":"商品名称","edit":"text","rules":"required"},
                {"field":"goods_type","title":"商品类型","edit":"select","rules":"required",searchList: Yunqi.data.goodstype},
                {'field':'coupon_id','title':'卡券',edit:{form: 'selectpage',url:'delivery/channel-coupon/index',keyField:'id',labelField:'title'},visible:function (row){return row.goods_type=='coupon'}},
                {'field':'stock','title':'库存',edit:{form: 'input',type:'number',value:9999,append: '件'},rules:'integer(+0)',visible:function (row){return row.goods_type=='entity'}},
                {"field":"price","title":"单价","edit":{form:'input',type:'number',append:'元'},"rules":"required;range(0~)"},
                {"field":"score_price","title":"积分抵扣","edit":{form:'input',type:'number',prepend:'每',append: '抵扣1元'},"rules":"range(0~)"},
                {"field":"max_score_money","title":"最多抵扣","edit":{form:'input',type:'number',append: '元'},"rules":"range(0~)"},
                {"field":"des","title":"商品简介",width:300,"edit":"textarea"},
                {"field":"img","title":"商品图片",width:120,"edit":"image","rules":"required"},
                {"field":"status","title":"状态","edit":"switch","searchList":{"normal":"正常","hidden":"隐藏"}},
            ],
            row:Yunqi.data.row || {
                goods_type:'entity'
            }
        },
        methods: {
            onSubmit:function (row) {
                if(row.goods_type=='coupon' && !row.coupon_id){
                    Yunqi.message.error('请选择优惠券');
                    this.$refs.yunform.setError('coupon_id','请选择优惠券');
                    return false;
                }
                if(row.goods_type!='coupon' && !row.stock){
                    Yunqi.message.error('库存为必填');
                    this.$refs.yunform.setError('stock','库存为必填');
                    return false;
                }
                if(row.goods_type=='coupon'){
                    delete row.stock;
                }
                if(row.max_score_money>row.price){
                    Yunqi.message.error('最多抵扣不能大于单价');
                    this.$refs.yunform.setError('max_score_money','最多抵扣不能大于单价');
                    return false;
                }
                return true;
            }
        }
    }
</script>
<style>
    
</style>

